package com.dwxt.sport.business.service;

import com.dwxt.common.base.StatusCode;
import com.dwxt.common.constant.VariableConstants;
import com.dwxt.common.util.SnowIdUtil;
import com.dwxt.sport.business.dao.AlarmHandleMapper;
import com.dwxt.sport.business.dao.AlarmInfoMapper;
import com.dwxt.sport.business.entity.AlarmHandle;
import com.dwxt.sport.business.entity.AlarmInfo;
import com.dwxt.sport.business.vo.alarm.InsertOrUpdateAlarmHandleVo;
import com.dwxt.sport.business.vo.alarm.InsertOrUpdateAlarmInfoVo;
import com.dwxt.sport.business.vo.alarm.SelectAlarmInfoVo;
import com.dwxt.sport.constant.CommonConstant;
import com.dwxt.sport.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * TODO
 *
 * @author Kuang.JiaZhuo
 * @date 2020-08-03 15:17
 */
@Service
public class AlarmService {

    @Resource
    AlarmInfoMapper infoMapper;

    @Resource
    AlarmHandleMapper handleMapper;

    /**
     * @方法名：insertOrUpdateAlarmInfo
     * @描述： 新增或修改报警信息
     * @作者： kjz
     * @日期： Created in 2020/8/3 16:00
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateAlarmInfo(InsertOrUpdateAlarmInfoVo insertOrUpdateVo) {
        String id = insertOrUpdateVo.getId();

        AlarmInfo alarmInfo = new AlarmInfo()
                .setName(insertOrUpdateVo.getName()) //设备名称
                .setUnitType(insertOrUpdateVo.getDeviceType()) //设备类型
                .setDeviceLocation(insertOrUpdateVo.getDeviceLocation()) //设备位置
                .setAlarmType(insertOrUpdateVo.getAlarmType()) //报警类型
                .setAlarmTime(insertOrUpdateVo.getAlarmTime()) //报警时间
                .setCauseOfAlarm(insertOrUpdateVo.getCauseOfAlarm()); //报警原因

        if(null==id || id.trim().length()==0){
            //新增
            String streetId = insertOrUpdateVo.getStreetId();
            if(null==streetId || streetId.trim().length()==0){
                throw new CustomException(StatusCode.STREET_ID_IS_NULL);
            }

            alarmInfo.setId(SnowIdUtil.getInstance().nextId())
                    .setCreateTime(LocalDateTime.now()).setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setStreetId(streetId) //街道id
                    .setAlarmState(VariableConstants.STRING_CONSTANT_1); //报警状态:新增时未处理
            return infoMapper.insert(alarmInfo);
        }else{
            //更新
            alarmInfo.setUpdateTime(LocalDateTime.now()).setId(id);
            return infoMapper.updateByPrimaryKeySelective(alarmInfo);
        }
    }

    /**
     * @方法名：selectAlarmInfoByCriteria
     * @描述： 按条件查询报警信息列表(带分页)
     * @作者： kjz
     * @日期： Created in 2020/8/3 17:09
     */
    public PageInfo<AlarmInfo> selectAlarmInfoByCriteria(SelectAlarmInfoVo selectVo) {
        PageHelper.startPage(selectVo.getPageNum(), selectVo.getPageSize());
        List<AlarmInfo> list = infoMapper.selectAlarmInfoByCriteria(selectVo);
        PageInfo<AlarmInfo> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * @方法名：selectAlarmInfoByPage
     * @描述： 查询所有报警信息列表(带分页)
     * @作者： kjz
     * @日期： Created in 2020/8/18 9:50
     */
    public PageInfo<AlarmInfo> selectAlarmInfoByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum==null?1:pageNum, pageSize==null?10:pageSize);
        List<AlarmInfo> list = infoMapper.selectAlarmInfoByAll();
        PageInfo<AlarmInfo> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * @方法名：deleteAlarmInfoByIds
     * @描述： 根据ids批量删除报警信息
     * @作者： kjz
     * @日期： Created in 2020/8/3 17:40
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteAlarmInfoByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");

        return this.infoMapper.deleteAlarmInfoByIds(idArray);
    }

    /**
     * @方法名：insertOrUpdateAlarmHandle
     * @描述： 新增 或 更新 报警处理
     * @作者： kjz
     * @日期： Created in 2020/8/3 18:09
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateAlarmHandle(InsertOrUpdateAlarmHandleVo insertOrUpdateVo) {
        String id = insertOrUpdateVo.getId();
        AlarmHandle alarmHandle = new AlarmHandle()
                .setHandler(insertOrUpdateVo.getHandler()) //处理人
                .setHandleTime(insertOrUpdateVo.getHandleTime()) //处理时间
                .setHandleDetails(insertOrUpdateVo.getHandleDetails()); //处理详情

        if (null == id || id.trim().length() == 0) {
            //新增
            String alarmInfoId = insertOrUpdateVo.getAlarmInfoId();
            if (null == alarmInfoId || alarmInfoId.trim().length() == 0) {
                throw new CustomException(StatusCode.ALARM_INFO_ID_IS_NULL);
            }
            alarmHandle.setId(SnowIdUtil.getInstance().nextId())
                    .setCreateTime(LocalDateTime.now()).setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setAlarmInfoId(alarmInfoId); //智慧场馆关联id

            return handleMapper.insert(alarmHandle);

        } else {
            //更新
            alarmHandle.setUpdateTime(LocalDateTime.now()).setId(id);
            return handleMapper.updateByPrimaryKeySelective(alarmHandle);
        }
    }

    /**
     * @方法名：deleteAlarmHandleByIds
     * @描述： 根据ids批量删除【报警处理】
     * @作者： kjz
     * @日期： Created in 2020/8/4 10:00
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteAlarmHandleByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");

        return this.handleMapper.deleteAlarmHandleByIds(idArray);
    }

    /**
     * @方法名：selectAlarmHandleByInfoId
     * @描述： 根据报警信息的id查询报警处理的详情
     * @作者： kjz
     * @日期： Created in 2020/8/18 11:15
     */
    public AlarmHandle selectAlarmHandleByInfoId(String alarmInfoId) {
        AlarmHandle alarmHandle = handleMapper.selectAlarmHandleByInfoId(alarmInfoId);
        if(alarmHandle==null){
            throw new CustomException(StatusCode.ALARM_HANDLE_IS_NULL);
        }
        String imgUrl = infoMapper.selectImgUrlById(alarmInfoId);
        alarmHandle.setImgUrl(CommonConstant.FILE_SERVER+imgUrl);
        return alarmHandle;
    }
}
